package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.Appointment;
import com.lizhi.yun.db.sfrobot.repository.AppointmentRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/11/13 8:59
 */
@Slf4j
@Service
public class RecvAppointmentHandler {

    @Resource
    private AppointmentRepo appointmentRepo;

    public String recvAppointment(String json){
        UpDataPOJO upClientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upClientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*预约信息*/
        Appointment appointment = SamJsonUtil.toObject(upClientPOJO.getData(), Appointment.class);
        if (StringUtils.isEmpty(appointment)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        appointment.setCreateTime(DateUtils.getNowDate());
        appointment.setHospitalId(rpcClientPOJO.getOrganId());
        appointment.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*查询是否存在*/
        Appointment example = new Appointment();
        example.setId(appointment.id);
        example.setHospitalId(rpcClientPOJO.getOrganId());
        example.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        Appointment recordFromDb = appointmentRepo.findOne(Example.of(example)).orElse(null);
        if (StringUtils.isEmpty(recordFromDb)){
            appointment.setLid(StringUtils.newId());
        } else {
            appointment.setLid(recordFromDb.lid);
        }
        log.info("上传的数据信息是appointment:{}", appointment);
        appointmentRepo.save(appointment);
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
